define(function (require, exports, module) {
    var angular = require('angular');
    var module = angular.module('directives.util', []);
    module.directive('ngMin', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            link: function (scope, elem, attr, ctrl) {
                scope.$watch(attr.ngMin, function () {
                    ctrl.$setViewValue(ctrl.$viewValue);
                });
                var minValidator = function (value) {
                    var min = scope.$eval(attr.ngMin) || 0;
                    if (!isEmpty(value) && value < min) {
                        ctrl.$setValidity('ngMin', false);
                        return undefined;
                    } else {
                        ctrl.$setValidity('ngMin', true);
                        return value;
                    }
                };

                ctrl.$parsers.push(minValidator);
                ctrl.$formatters.push(minValidator);
            }
        };
    });
    module.directive("dblength", function () {
        function getLength(min, max) {
            return function (val) {
                var l = !val ? 0 : (function len(s) {
                    var l = 0;
                    var a = s.split("");
                    for (var i = 0; i < a.length; i++) {
                        if (a[i].charCodeAt(0) < 299) {
                            l++;
                        } else {
                            l += 2;
                        }
                    }
                    return l;
                })(val);
                return l >= min && l <= max;
            };
        }

        return {
            require: 'ngModel',
            link: function (scope, iElement, iAttrs, ctrl) {
                var ml = scope.$eval(iElement.attr("dblength"));
                var min = 0;
                var max = parseInt(ml) || 255;
                var validate = getLength(min, max);
                ctrl.$parsers.push(function (val) {
                    ctrl.$setValidity('maxdblength', validate(val));
                    return val;
                });
            }
        };
    });
});